import sys
from bs4 import BeautifulSoup
import json
from util import py_dl
from util import py_socket


def parse_url(url, is_file=False):
    detail = dict()
    detail['url'] = url
    if is_file:
        soup = BeautifulSoup(open(url, encoding='utf-8'), 'lxml')
    else:
        html_doc = py_dl.dl_url(url)
        soup = BeautifulSoup(html_doc, 'lxml')
    # detail['title'] = soup.title.string
    detail['label'] = soup.find(class_='info').contents[1].string
    detail['detail'] = soup.find(class_='played2k').contents[1].string
    py_socket.send_msg(json.dumps(detail))


# parse_url('https://444av.vip/vod/13320.html')

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('缺少参数！')
    else:
        input_url = sys.argv[1]
        if input_url.startswith('http'):
            parse_url(input_url)
        else:
            parse_url(input_url, True)
